package com.zkingsoft.services.distribution.impl;

import com.zkingsoft.services.distribution.DistributionProductService;
import com.zkingsoft.sqlUtil.ServiceUtil;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.distribution.DistributionImages;
import com.zkingsoft.model.distribution.DistributionProduct;
import com.zkingsoft.dao.distribution.DistributionImagesDao;
import com.zkingsoft.dao.distribution.DistributionProductDao;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-06-16 15:22
 */
@Service("distributionProductService")
public class  DistributionProductServiceImpl implements    DistributionProductService  {

	
	@Autowired
	private DistributionProductDao distributionProductDao;
	@Autowired
	private DistributionImagesDao distributionImagesDao;
	@Autowired
	private ServiceUtil serviceUtil;
	@Override
	public int add(DistributionProduct distributionProduct){
//		if (serviceUtil.addCheckRepeat("dis_product", "pro_name", 
//				distributionProduct.getProName())) {
//			throw new GlobleException("产品名称已经存在");
//		}
		return distributionProductDao.insert(distributionProduct);
		
	}
	
	@Override
	public int batchAdd(List<DistributionProduct>  distributionProductList) {
		int num = 0;

		int c = 10000;
		int size = distributionProductList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= distributionProductList.size() ? distributionProductList.size() : end;
			List<DistributionProduct> insertList = distributionProductList.subList(begin, end);
			num += distributionProductDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(DistributionProduct oldDistributionProduct
	,DistributionProduct newDistributionProduct){
	
		if (!ModelUtils.isModified(oldDistributionProduct, newDistributionProduct)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDistributionProduct, newDistributionProduct);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("proId", oldDistributionProduct.getProId());
			distributionProductDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(DistributionProduct distributionProduct){
	
		return distributionProductDao.updateByModel(distributionProduct);
	
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public int remove(List<Long> list){
	for(Long proId:list){
		DistributionImages disImg=new DistributionImages();
		disImg.setProId(proId);
		distributionImagesDao.deleteByModel(disImg);
	}
		return distributionProductDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long proId){
	
		return distributionProductDao.deleteById(proId);
	
	}
	
	@Override
	public int removeByModel(DistributionProduct distributionProduct){
	
		return distributionProductDao.deleteByModel(distributionProduct);
	
	}
	
	
	@Override
	public List<DistributionProduct> findInPage(DistributionProduct distributionProduct,  PaginationVO pageVo){
	
		return distributionProductDao.selectInPage(distributionProduct , pageVo);
	
	}
	
	@Override
	public List<DistributionProduct> findByModel(DistributionProduct distributionProduct){
	
		return distributionProductDao.selectByModel(distributionProduct);
	
	}
	
	@Override
	public int  findTotal(DistributionProduct distributionProduct){
	
		return distributionProductDao.selectTotalRecord(distributionProduct);
	
	}
	
	@Override
	public DistributionProduct  findById(Long proId){
	
		return distributionProductDao.selectById(proId);
	
	}
   	
	@Override
	public List<DistributionProduct> findAppGoodList(String queryText, PaginationVO pageVo, Long parentId, Long childId) {

		return distributionProductDao.selectAppGoodList(queryText, pageVo, parentId, childId);
	}
	
	
}